package com.alibaba.basic.controller;

import com.alibaba.basic.dto.BinderDto;
import com.alibaba.basic.dto.SmsDto;
import com.alibaba.basic.exception.BusinessException;
import com.alibaba.basic.service.IVerifyService;
import com.alibaba.basic.util.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 验证码接口
 * 1.图形验证码接口
 * 2.手机验证码接口
 */
@RestController
@RequestMapping("/verifyCode")
public class VerifyCodeController {

    @Autowired
    private IVerifyService verifyService;

    @RequestMapping("/image/{key}")
    public AjaxResult imageVerifyCode(@PathVariable("key") String key){

        try {
            String base64str = verifyService.getImageCode(key);
            return AjaxResult.me().setResultObj(base64str);
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"获取验证码失败");
        }
    }

    /**
     * 获取手机验证码接口
     * @param smsDto
     * @return
     */
    @PostMapping("/smsCode")
    public AjaxResult sendSmsCode(@RequestBody SmsDto smsDto){

        try {
            verifyService.sendSmsCode(smsDto);
            return new AjaxResult();
        } catch (BusinessException e) {
            e.printStackTrace();
            return new AjaxResult(false,"发送失败!"+e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"获取验证码失败");
        }
    }

    /**
     * 获取手机验证码接口
     * @param binderDto
     * @return
     */
    @PostMapping("/binderSmsCode")
    public AjaxResult getBinderSmsCode(@RequestBody BinderDto binderDto){

        try {
            verifyService.getBinderSmsCode(binderDto);
            return new AjaxResult();
        } catch (BusinessException e) {
            e.printStackTrace();
            return new AjaxResult(false,"发送失败!"+e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"获取验证码失败");
        }
    }

}
